package com.htj.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.htj.admin.domain.*;
import com.htj.admin.service.FoodTypeServiceInf;
import com.htj.admin.service.FrontUserServiceInf;
import com.htj.admin.service.VIPServiceInf;
import com.htj.common.Result;
import com.htj.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "vip")
@CrossOrigin
public class VIPController {
    @Autowired
    VIPServiceInf vipServiceInf;
    @Autowired
    FrontUserServiceInf frontUserServiceInf;
    /**
     * 新增vip
     * @param
     * @return
     */
    @PostMapping("/save")
    public Result<String> add(@RequestBody VIP vip){
        boolean bool = vipServiceInf.add(vip);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 修改vip信息
     * @param
     * @return
     */
    @PutMapping("/save")
    public Result<String> update(@RequestBody VIP vip){
        boolean bool = vipServiceInf.update(vip);
        return bool?Result.succ():Result.fail();
    }
    /**
     * 分页数据
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/pageList")
    public Result<PageListVo> pageList(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String name){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("name",name);
        PageListVo<VIP> pageList = vipServiceInf.pageList(map);
        return Result.succ(pageList);
    }

    /**
     * 删除vip
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        boolean bool = vipServiceInf.delete(id);
        return bool?Result.succ():Result.fail();
    }
    /**
     * 批量删除
     * @param list
     * @return
     */
    @PostMapping("/deleteByIds")
    public Result<String> deleteByIds(@RequestBody List<Integer> list){
        boolean bool = true;
        for(Integer id : list){
            bool = vipServiceInf.delete(id);
        }
        return bool?Result.succ():Result.fail();
    }

    /**
     * 获得所有vip
     * @return
     */
    @GetMapping("/getVIP")
    public Result<List<VIP>> getVIP(){
        List<VIP> list = vipServiceInf.getVIP();
        return Result.succ(list);
    }
    @GetMapping("/getMyVip")
    public Result<VIP> getMyVip(@RequestParam String id){
        VIP vip = vipServiceInf.getMyVip(id);
        return Result.succ(vip);
    }

    /**
     * 这个id是会员id,给用户开通哪一个级别的会员
     * @param id
     * @return
     */
    @GetMapping("/buy")
    public Result<?> buy(@RequestParam String id,@RequestParam String userId) {
        VIP vip = vipServiceInf.getById(id);
        FrontUser frontUser = frontUserServiceInf.getUserById(userId);
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        String payUrl = "http://localhost:9001/order/alipay/pay?subject=" + "v" + vip.getId()+"u"+frontUser.getId() + "&traceNo=" + orderNo + "&totalAmount=" + vip.getPrice();
        // 新建订单，扣减库存
        return Result.succ(payUrl);
    }
}
